/**
 * Created by Administrator on 15-10-8.
 */
window.onload=function() {
    var id = $.getUrlParam("id");
    var userId = getUserId();
    var samewayId = $.getUrlParam('samewayId')
    $("#jubao").attr('href', 'jubao.html?objId=' + samewayId + "&type=1");

    //详细行程
    var url = getUrl();
    $.jsonp({
        url: 'http://' + url + '/api/sameway/detail-' + id,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            var nickName = data.user.nickName;
            $("#nickName").text(nickName);
            if(data.user.sex==1){
                $("#sexicon").addClass("icons-sex-male");
                $("#sex").addClass("sex sex-male");

            }else{
                $("#sexicon").addClass("icons-sex-female");
                $("#sex").addClass("sex sex-female");
            }
            var age = data.user.age;
            $("#age").text(age);
            var photo = data.user.photo;
            $("#photo").attr('src', photo);
            var charisma = data.user.charisma;
            $("#charisma").text(charisma);
            var startTime = data.sameway.startTime;
            $("#startTime").text(getTime(data.sameway.startTime));
            var endTime = data.sameway.endTime;
            $("#endTime").text(getTime(data.sameway.endTime));
            var wayType = data.sameway.wayType;
            $("#wayType").text(wayType);
            var goAddress = data.sameway.goAddress;
            $("#goAddress").text(goAddress);
            var targetSpots = data.sameway.targetSpots;
            $("#targetSpots").text(targetSpots);
            var rentType = data.sameway.rentType;
            $("#rentType").text(rentType);
            var wayNumber = data.sameway.wayNumber;
            $("#wayNumber").text(wayNumber);
            var contact = data.sameway.contact;
            $("#contact").text(contact);
            var brightSpot = data.sameway.brightSpot;
            $("#brightSpot").text(brightSpot);
            var wayDesc = data.sameway.wayDesc;
            $("#wayDesc").text(wayDesc);
            var photos = data.sameway.photos[0];
            $("#photos").attr('src',photos);
            $(".edit").attr('data-gz', data.user.id);
            $(".log-bm").attr('data-bm', id);
            $(".log-sc").attr('data-sc', samewayId);

            //判断是否关注
            var tadeuserid = data.user.id;
            $.jsonp({
                url: 'http://' + url + '/api/user/user_cluck_list',
                data: {userId: userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {

                    $.each(data.arrays, function (index, ele) {
                        if (ele.cluckId == tadeuserid) {
                            $(".edit").text("已关注").css({'color': '#c5c5c5', 'border': '1px solid #c5c5c5'});
                        }
                    });
                    $(".edit").show();
                },
                error: function (xOptions, textStatus) {
                }

            });
            //判断是否报名
            $.jsonp({
                url: 'http://' + url + '/api/sameway/assist_sameway-' + userId,
                data: {userId: userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays, function (index, ele) {
                        if (ele.id == id) {
                            $(".log-bm").text("已报名")

                        }


                    });
                    $("#bottomul").show();
                },
                error: function (xOptions, textStatus) {
                }

            });
            //判断是否收藏
            $.jsonp({
                url: 'http://' + url + '/api/collect/collect_sameway_list',
                data: {userId: userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays, function (index, ele) {
                        //var id = ele.samewayId;
                        if (id == ele.samewayId) {
                            $(".log-sc").addClass("btn").text("已收藏").css({'font-size': '14px', 'color': '#fff'});
                        }
                    });
                    $("#boot").show();
                },
                error: function (xOptions, textStatus) {
                }
            });
        },

        error: function (xOptions, textStatus) {
        }
    });
    //添加查看人数
    $.jsonp({
        url: 'http://' + url + '/api/sameway/look_sameway-' + id,
        data: {userId: userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
        },

        error: function (xOptions, textStatus) {
        }
    });
    //看过的人
    $.jsonp({
        url: 'http://' + url + '/api/sameway/look_number-' + id,
        data: {userId: userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#looknumber").text(data.lookNumber);

            $.each(data.arrays, function (index, ele) {
                var sex = "female";
                if (ele.sex == 1)
                    sex = "male";
                var html = '<ul class="order-list">' +
                    '<li>' +
                    '<div class="order-list-avatar">' +
                    '<img src="' + ele.photo + '"alt="">' +
                    '</div>' +
                    '<div class="order-list-intro">' +
                    '<div class="order-list-intro-text">' +
                    '<p class="color666 fz16">' + ele.nickName + '</p>' +
                    '<p class="color666 fz14">' +
                    '<span class="sex sex-' + sex + ' no-left">' +
                    '<i class="icons-sex-' + sex + '"></i>' + ele.age + '</span>' +
                    '</p>' +
                    '</div>' +
                    '</div>' +
                    ' </li>' +
                    '</ul>';
                $(".ma_con02 ").append(html)
            })
        },

        error: function (xOptions, textStatus) {
        }
    });
    var id = $.getUrlParam("id");
    //评论过的人
    $.jsonp({
        url: 'http://'+url+'/api/sameway/comment_list-'+id,
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#commentSize").text(data.comentNumber);
            $.each(data.arrays,function(index,ele){
                (function (){
                    var n = new Date().getTime();
                    o = n-ele.createTime*1000;
                    ele.createTime = ago(o);//返回距今时间
                    //传入时间毫秒 如果是时间戳 *1000
                })();
                var sex="female";
                if(ele.sex==1)
                    sex="male";
                var html='<ul class="comment-list">' +
                    '<li class="ui-border-b">' +
                    '<div class="comment-list-avatar">' +
                    '<img src="'+ele.photo+'"width="40" height="40" alt="">' +
                    '</div>' +
                    '<div class="comment-list-content">' +
                    '<div class="comment-list-content-text">' +
                    '<div class="clearfix"><div class="comment-list-content-user">'+ele.nickName+'' +
                    '<span class="sex sex-'+sex+' no-left"><i class="icons-sex-'+sex+'"></i>'+ele.age+'</span>' +
                    '</div>' +
                    '<div class="comment-list-content-time">' +
                    '<p>'+ele.createTime+'</p>' +
                    '</div>' +
                    '</div>' +
                    '<p class="color666 fz12">'+ele.comment+'</p>' +
                    '</div>' +
                    '</div>' +
                    '</li>' +
                    '</ul>';
                $(".ma_con03").append(html)

            })
        },

        error: function (xOptions, textStatus) {
        }
    });
    var id = $.getUrlParam("id");
    //已报名的人数
    $.jsonp({
        url: 'http://'+url+ '/api/sameway/assist_sameway-'+userId,//接口不对
        data: {userId: userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {

            //报名人数
            $("#enterIds").text(data.arrays.length);
            $.each(data.arrays, function (index, ele) {
                var sex = "female";
                if (ele.sex == 1)
                    sex = "male";
                var html = '<ul class="order-list order-list-grey">' +
                    '<li>' +
                    '<div class="order-list-avatar">' +
                    '<img src="' + ele.photo + '" alt="">' +
                    '</div>' +
                    '<div class="order-list-intro">' +
                    '<div class="order-list-intro-text">' +
                    '<p class="color666 fz16">' + ele.nickName + '</p>' +
                    '<p class="color666 fz14"><span class="sex sex-' + sex + ' no-left"><i class="icons-sex-' + sex + '"></i>' + ele.age + '</span></p>' +
                    '</div>' +
                    '</div>' +
                    ' </li>' +
                    '</ul>';
                $(".ma_con04").append(html)
            })
        },

        error: function (xOptions, textStatus) {
        }
    });
}
function ago(o){
    var times =o;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var year = month * 365;
    var yearC = times/year;
    var monthC =times/month;
    var dayC =times/day;
    var hourC =times/hour;
    var minC =times/minute;
    var html ="";
    if(yearC>=1){
        html += parseInt(yearC) + "年前";
        return html;
    }else if(monthC>=1){
        html += parseInt(monthC) + "月前";
        return html;
    }else if(dayC>=1){
        html += parseInt(dayC) + "天前";
        return html;
    }else if(hourC>=1){
        html += parseInt(hourC) + "个小时前";
        return html;
    }else if(minC>=1){
        html += parseInt(minC) + "分钟前";
        return html;
    }else{
        return "刚刚";
    }
}
//function onBackKeyDown() {
//    jumpbyback(0,"");
//}
function getTime(t){
    var nt = t*1000;
    var n = new Date(nt);
    return formatDate(n);
}
function formatDate(now)   {
    var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    var   date=now.getDate();
    return   year+"-"+(month>=10?month:"0"+month)+"-"+(date>=10?date:"0"+date);
}